package com.snack.shop.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.common.Result;
import com.snack.shop.dto.CreatePickupOrderParam;
import com.snack.shop.service.PickupOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 代取订单控制器
 */
@Tag(name = "代取订单管理")
@RestController
@RequestMapping("/pickup/order")
@RequiredArgsConstructor
public class PickupOrderController {

    private final PickupOrderService pickupOrderService;

    @Operation(summary = "创建代取订单")
    @PostMapping("/create")
    public Result<Map<String, Object>> create(@Validated @RequestBody CreatePickupOrderParam param) {
        Long userId = StpUtil.getLoginIdAsLong();
        Long orderId = pickupOrderService.createOrder(param, userId);
        
        Map<String, Object> data = new HashMap<>();
        data.put("id", orderId);
        data.put("message", "订单创建成功");
        return Result.success(data);
    }

    @Operation(summary = "获取订单列表（分页）")
    @GetMapping("/page")
    public Result<Page<Map<String, Object>>> page(
            @Parameter(description = "角色：publisher-发布方，taker-接单方，merchant-商家") @RequestParam String role,
            @Parameter(description = "订单状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "结算状态：0-未结算，1-申请中，2-已结算") @RequestParam(required = false) Integer settlementStatus,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        Page<Map<String, Object>> page = pickupOrderService.getOrderPage(role, status, settlementStatus, userId, current, size);
        return Result.success(page);
    }

    @Operation(summary = "获取可接订单列表")
    @GetMapping("/available")
    public Result<Page<Map<String, Object>>> available(
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Page<Map<String, Object>> page = pickupOrderService.getAvailableOrders(categoryId, current, size);
        return Result.success(page);
    }

    @Operation(summary = "获取订单详情")
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getDetail(@PathVariable Long id) {
        Map<String, Object> detail = pickupOrderService.getOrderDetail(id);
        return Result.success(detail);
    }

    @Operation(summary = "接单")
    @PostMapping("/{id}/accept")
    public Result<String> accept(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        pickupOrderService.acceptOrder(id, userId);
        return Result.success("接单成功");
    }

    @Operation(summary = "确认取件")
    @PostMapping("/{id}/pickup")
    public Result<String> pickup(
            @PathVariable Long id,
            @RequestBody(required = false) Map<String, String> body
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        String pickupImages = body != null ? body.get("pickupImages") : null;
        pickupOrderService.confirmPickup(id, userId, pickupImages);
        return Result.success("确认取件成功");
    }

    @Operation(summary = "确认送达")
    @PostMapping("/{id}/deliver")
    public Result<String> deliver(
            @PathVariable Long id,
            @RequestBody(required = false) Map<String, String> body
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        String deliveryImages = body != null ? body.get("deliveryImages") : null;
        pickupOrderService.confirmDeliver(id, userId, deliveryImages);
        return Result.success("确认送达成功");
    }

    @Operation(summary = "确认收货")
    @PostMapping("/{id}/confirm")
    public Result<String> confirm(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        pickupOrderService.confirmReceive(id, userId);
        return Result.success("确认收货成功");
    }

    @Operation(summary = "取消订单")
    @PostMapping("/{id}/cancel")
    public Result<String> cancel(
            @PathVariable Long id,
            @RequestBody Map<String, String> body
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        String reason = body.get("reason");
        pickupOrderService.cancelOrder(id, userId, reason);
        return Result.success("订单已取消");
    }
}

